const initialState = {
  list: [
    { id: 1, task: '学习TS', isDone: true },
    { id: 2, task: '学习React', isDone: false },
    { id: 3, task: '学习Vue3', isDone: true },
  ],
  type: 'all',
};

const todoReducer = (state = initialState, { type, payload }) => {
  console.log('type, payload  ----->  ', type, payload);
  switch (type) {
    case 'delById':
      return {
        ...state,
        list: state.list.filter((item) => item.id !== payload),
      };

    case 'updateById':
      return {
        ...state,
        list: state.list.map((item) => ({
          ...item,
          isDone: item.id === payload ? !item.isDone : item.isDone,
        })),
      };

    case 'add':
      return {
        ...state,
        list: [payload, ...state.list],
      };

    case 'updateType':
      return { ...state, type: payload };
    default:
      return state;
  }
};

export default todoReducer;
